<?php

namespace App\Admin\Actions\Order;
use App\Models\yb\NetworkCreditRecordModel;
use App\Models\yb\NetworkGoodsModel;
use App\Models\yb\NetworkGoodsOrderModel;
use App\Models\yb\NetworkReturnBucketModel;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;


class ReturnBucketAudio extends RowAction
{
    public $name = '审核';

    /**
     * @param Model $model
     * @return string
     */
    public function handle(Model $model, Request $request)
    {
        $type = $request->get('type');

        $content = $request->get('content');

        if($type == 1) {
            if($model->total_barrel_deposit>0){
                NetworkGoodsModel::where(['network_id'=>$model->network_id,'goods_id'=>$model->goods_id])->increment('barrel_deposit',$model->total_barrel_deposit);
                NetworkCreditRecordModel::insert([
                    'network_id' => $model->network_id,
                    'goods_id'=> $model->goods_id,
                    'number' => $model->bucket_number,
                    'amount' => $model->total_barrel_deposit,
                    'remark' => '还桶增加桶押金',
                    'status' => 1,
                    'relation_data' => $model->order_sn,
                    'type' => 6,
                    'created_at' => date('Y-m-D H:i:s',time())
                ]);
            }
        }
        NetworkReturnBucketModel::where(['id'=>$model->id])->update([
            'status' => $type,
            'remark' => $content
        ]);

        return $this->response()->success('操作成功')->refresh();

    }

    /**
     * @param $star
     * @return string
     */
    public function display($id)
    {
        return  '<button type="button" class="btn btn-xs btn-info btn-flat">审核</button>';
    }


    public function form()
    {
        $this->radio('type', '类型')->options([1 => '通过',2=>'驳回'])->required()->default(1);

        $this->textarea('content','备注');

    }

}